(function($){
    $.fn.terminal = {
        "data": [
            {
                //mobile端首页头图
                "name": "mobileIndexHead",
                "terminal": 3,
                "type": 1,
                "subscribe": 0,
                "imgW": 248,
                "imgH": 140
            },
            {
                //mobile端首页列表
                "name": "mobileIndexFocus",
                "terminal": 3,
                "type": 1,
                "subscribe": 2,
                "imgW": 248,
                "imgH": 82
            },
            {
                //mobile端直播列表
                "name": "mobileLiveFocus",
                "terminal": 3,
                "type": 100,
                "subscribe": 2,
                "imgW": 248,
                "imgH": 82
            },
            {
                //mobile端秀场列表
                "name": "mobileShowFocus",
                "terminal": 3,
                "type": 2,
                "subscribe": 2,
                "imgW": 248,
                "imgH": 82
            },
            {
                //ott端首页列表
                "name": "ottIndexFocus",
                "terminal": 1,
                "type": 1,
                "subscribe": 2,
                "imgW": 320,
                "imgH": 180
            },
            {
                //ott端直播列表
                "name": "ottLiveFocus",
                "terminal": 1,
                "type": 100,
                "subscribe": 2,
                "imgW": 320,
                "imgH": 180
            },
            {
                //ott端秀场列表
                "name": "ottShowFocus",
                "terminal": 1,
                "type": 2,
                "subscribe": 2,
                "imgW": 320,
                "imgH": 180
            }
        ]
    };

    $.fn.ott=function(options,sets){
        options = $.extend({
            id:"",
            focusName:"focusName",
            type:1,// 1 首页 2 音乐节 3 演唱会 4 Live House 5 MTV 6 排行 100 直播 999 子类
            subscribe:0,// 0 推荐位 1 预约 2 热门栏目 3 专题
            terminal:"3",//终端  1 TV&OTT  2 PC 3 Mobile 4 pad 5 全端
            objectId:"",
            displayOrder:10,
            data:"",
            description:""
        },options);

        sets = $.extend({
            save:".save",//保存按钮
            preview:".preview",
            focus:".myFocus",
            mobileHead:".cms-mobile-head",
            mobileFocus:".cms-mobile-focus",
            previewImage:".cms-preview-image",
            category:{},
            terminal:{},

            indexUrl:"/vrs/focus/index.html",
            saveUrl:"/vrs/focus/save.jsn",

            imgW:248,
            imgH:140
        },sets);




        var _this = $(this);
        var _ul = $("ul",_this);
        var _save = $(sets.save,_this);
        var _preview = $(sets.preview,_this);
        var _img = $(".add-video",_this);
        var _cmsDiv = $("div",_this);
        var _dataDiv = $(".cms-images",_this);
        var _btnDiv = $(".cms-btn",_this);
        var _navDivF = $(".cms-nav-father",_this);
        var _navDiv = $(".cms-nav",_this);
        var _gory = $(".gory",_this);
        var _label = $("label",_this);
        var _previewImage = $(sets.previewImage);

        //初始化列表数据
        //initEditIndexList(_ul.attr("id"),options.data,sets.category);

        var imgW = '';
        var imgH = '';
        _previewImage.each(function(){
            imgW = $(this).attr("width");
            imgH = $(this).attr("height");
        });
        var css = {
            dataDiv:{width:imgW,height:1.5*imgW},
            img:{width:imgW,height:imgH},
            gory:{right:-(imgW-100)}
        };

        //是图片div与图片自适应
        _dataDiv.css(css.dataDiv);
        _btnDiv.css({width:imgW});
        _navDiv.css({width:imgW});
        _navDivF.css({width:imgW});
        _gory.css(css.gory);
        _img.each(function(){
            $(this).css(css.img);
        });//(css.img);

        //初始化导航栏
        _navDivF.children().each(function(){
            var _thisLabel = $(this);
            _thisLabel.removeClass('active');
            if(options.terminal == _thisLabel.attr("terminal")){
                _thisLabel.addClass('active');
            }
        });
        _navDiv.children().each(function(){
            var _thisLabel = $(this);
            _thisLabel.removeClass('active');
            if(options.type == _thisLabel.attr("type") &&
                options.subscribe == _thisLabel.attr("subscribe")&&
                options.terminal == _thisLabel.attr("terminal")){
                _thisLabel.addClass('active');
            }
        });

        _label.each(function(){
            var _thisLabel = $(this);
            _thisLabel.click(function(){
                //window.location.href = '/vrs/focus/index/'+_thisLabel.attr('terminal')+'.html?type='+_thisLabel.attr('type')+'&subscribe='+_thisLabel.attr('subscribe');
                window.location.href = sets.indexUrl+'?terminal='+_thisLabel.attr('terminal')+'&type='+_thisLabel.attr('type')+'&subscribe='+_thisLabel.attr('subscribe');
            })
        });
        //预览
        _preview.bind("click",function(){
            var data = getProviewData();
            console.log(data);
            if(data == ''){
                return;
            }
            if(options.terminal ==1){//ott
                if(options.type==7){
                    initPreviewBackgroundList($(sets.mobileFocus),data);
                }else{
                    initEditOttList($(sets.focus),data);
                }
            }else if(options.terminal == 5){//排行榜
                console.log($.toJSON(options),data);
                initPreviewRankList($(sets.mobileFocus),data);
            }else{//手机端
                if(options.type == 100 && options.subscribe == 0){
                    initPreviewMobileHead($(sets.mobileHead),data);
                }else{
                    initPreviewMobileList($(sets.mobileFocus),data,sets.category);
                }

            }
        });

        //保存按钮
        _save.bind("click",function(){
            var _label = $('.active',_navDiv);


            options.id = _ul.attr("data-id");
            options.type = _label.attr("type");
            options.subscribe = _label.attr("subscribe");
            options.terminal = _label.attr("terminal");
            console.log('options',options);
            //加载图片数据
            var data = getProviewData();
            console.log(data);
            if(data == ''){
                return;
            }
            options.data = data;
            //console.log(options);
            $.post(sets.saveUrl,options,function(data,status){

                var success = data.success;
                var msg = data.msg;
                if(status == "success"){
                    swal({
                        title: data.msg,
                        text: '点击返回',
                        type:'success'
                    });
                }else{
                    swal({
                        title: '保存失败',
                        text: '点击返回',
                        type:'success'
                    });
                }

            });
        });

        function getProviewData(){
            var array = new Array();
            var flag = true;
            $('input.data-json',_ul).each(function(i){
                var json = $(this).val();
                if(json == ''){
                    var msg = '请查看数据是否完整。   ';
                    var type = 'error';
                    var $layout = 'topRight';
                    notyRestart(msg,type,$layout);
                    flag = false;
                    return;
                }
                json = $.evalJSON(json);
                array[i]=json;
            })
            if(!flag){
                return '';
            }
            var data = {total:array.length,rows:array};
            console.log(data);
            return $.toJSON(data);
        }


        //if(options.terminal ==1){
        //    initEditOttList($(sets.focus), $.toJSON(sets.category));
        //}

    };
})(jQuery);